package likou.jindian;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*

[]
[]
 */
/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/06/02 08:21
 */
public class _17_07 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(trulyMostPopular(
                new String[]{"Fcclu(70)","Ommjh(63)","Dnsay(60)","Qbmk(45)","Unsb(26)","Gauuk(75)","Wzyyim(34)","Bnea(55)","Kri(71)","Qnaakk(76)","Gnplfi(68)","Hfp(97)","Qoi(70)","Ijveol(46)","Iidh(64)","Qiy(26)","Mcnef(59)","Hvueqc(91)","Obcbxb(54)","Dhe(79)","Jfq(26)","Uwjsu(41)","Wfmspz(39)","Ebov(96)","Ofl(72)","Uvkdpn(71)","Avcp(41)","Msyr(9)","Pgfpma(95)","Vbp(89)","Koaak(53)","Qyqifg(85)","Dwayf(97)","Oltadg(95)","Mwwvj(70)","Uxf(74)","Qvjp(6)","Grqrg(81)","Naf(3)","Xjjol(62)","Ibink(32)","Qxabri(41)","Ucqh(51)","Mtz(72)","Aeax(82)","Kxutz(5)","Qweye(15)","Ard(82)","Chycnm(4)","Hcvcgc(97)","Knpuq(61)","Yeekgc(11)","Ntfr(70)","Lucf(62)","Uhsg(23)","Csh(39)","Txixz(87)","Kgabb(80)","Weusps(79)","Nuq(61)","Drzsnw(87)","Xxmsn(98)","Onnev(77)","Owh(64)","Fpaf(46)","Hvia(6)","Kufa(95)","Chhmx(66)","Avmzs(39)","Okwuq(96)","Hrschk(30)","Ffwni(67)","Wpagta(25)","Npilye(14)","Axwtno(57)","Qxkjt(31)","Dwifi(51)","Kasgmw(95)","Vgxj(11)","Nsgbth(26)","Nzaz(51)","Owk(87)","Yjc(94)","Hljt(21)","Jvqg(47)","Alrksy(69)","Tlv(95)","Acohsf(86)","Qejo(60)","Gbclj(20)","Nekuam(17)","Meutux(64)","Tuvzkd(85)","Fvkhz(98)","Rngl(12)","Gbkq(77)","Uzgx(65)","Ghc(15)","Qsc(48)","Siv(47)"},
                new String[]{"(Gnplfi,Qxabri)","(Uzgx,Siv)","(Bnea,Lucf)","(Qnaakk,Msyr)","(Grqrg,Gbclj)","(Uhsg,Qejo)","(Csh,Wpagta)","(Xjjol,Lucf)","(Qoi,Obcbxb)","(Npilye,Vgxj)","(Aeax,Ghc)","(Txixz,Ffwni)","(Qweye,Qsc)","(Kri,Tuvzkd)","(Ommjh,Vbp)","(Pgfpma,Xxmsn)","(Uhsg,Csh)","(Qvjp,Kxutz)","(Qxkjt,Tlv)","(Wfmspz,Owk)","(Dwayf,Chycnm)","(Iidh,Qvjp)","(Dnsay,Rngl)","(Qweye,Tlv)","(Wzyyim,Kxutz)","(Hvueqc,Qejo)","(Tlv,Ghc)","(Hvia,Fvkhz)","(Msyr,Owk)","(Hrschk,Hljt)","(Owh,Gbclj)","(Dwifi,Uzgx)","(Iidh,Fpaf)","(Iidh,Meutux)","(Txixz,Ghc)","(Gbclj,Qsc)","(Kgabb,Tuvzkd)","(Uwjsu,Grqrg)","(Vbp,Dwayf)","(Xxmsn,Chhmx)","(Uxf,Uzgx)"})));
    }

    public static String[] getStrs(String str) {
        String regex = "(\\w+,\\w+)";
        Pattern pat = Pattern.compile(regex);
        Matcher matcher = pat.matcher(str);
        boolean b = matcher.find();
        return matcher.group(0).split(",");
    }

    public static int getNum(String str) {
        String regex = "(\\d+)";
        Pattern pat = Pattern.compile(regex);
        Matcher matcher = pat.matcher(str);
        boolean b = matcher.find();
        return Integer.parseInt(matcher.group(0));
    }

    public static String getName(String str) {
        String regex = "(\\(\\d+\\))";
        Pattern pat = Pattern.compile(regex);
        return pat.split(str)[0];
    }

    static Map<String, String> nameMap = new HashMap<>();//key是名字，value是父亲
    static Map<String, Integer> resMap = new HashMap<>();//key是名字，value是数量

    public static String[] trulyMostPopular(String[] names, String[] synonyms) {
        for (String str : synonyms) {
            String[] strs = getStrs(str);
            String father1 = strs[0];
            if (nameMap.containsKey(father1)) {
                father1 = getFather(father1);
            }
            String father2 = strs[1];
            if (nameMap.containsKey(father2)) {
                father2 = getFather(father2);
            }
            //father1大于father2，father2为father1的父亲
            if (father1.compareTo(father2) > 0) {
                nameMap.put(father1, father2);
                nameMap.put(father2, father2);
            } else {
                nameMap.put(father2, father1);
                nameMap.put(father1, father1);
            }
        }
        for (String str : names) {
            String name = getName(str);
            String father = nameMap.get(name);
            if (father==null){
                resMap.merge(name, getNum(str), Integer::sum);
                continue;
            }
            resMap.merge(father, getNum(str), Integer::sum);
        }
        String[] res = new String[resMap.size()];
        int index = 0;
        for (Map.Entry<String, Integer> entry : resMap.entrySet()) {
            res[index++] = entry.getKey() + "(" + entry.getValue() + ")";
        }
        return res;
    }

    public static String getFather(String name) {
        if (name.equals(nameMap.get(name))) {
            return name;
        }
        nameMap.put(name, getFather(nameMap.get(name)));
        return nameMap.get(name);
    }
}
